package com.mingliu;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@EnableAsync
@Configuration
public class AppConfig {

    @Bean("async")
    public SpringAsync async() {
        return new SpringAsync();
    }


    /**
     * Spring项目下如果没有手动定义ThreadPoolTaskExecutor为taskExecutor，那么会默认使用
     *
     * SimpleAsyncTaskExecutor  这不是线程池，这是一个不断创建线程的工厂
     * @return
     */
    @Lazy
    @Bean(
            name = {"applicationTaskExecutor", "taskExecutor"}
    )
    public ThreadPoolTaskExecutor applicationTaskExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        return taskExecutor;
    }
}
